import request from '@/utils/request'
import type { Response, PageInstanceVO } from '@/api/type'
import { AssignRoleParam, UserVO } from './type'
import { RoleInfoVO } from '../role/type'

enum API {
  USER_LIST_URL = '/admin/acl/user/list',// 用户列表
  DELETE_USER_URL = '/admin/acl/user/remove',// 删除用户
  ADD_USER_URL = '/admin/acl/user/save',// 添加用户
  UPDATE_USER_URL = '/admin/acl/user/update',// 修改用户
  ASSIGN_ROLE_URL = '/admin/acl/user/doAssign',// 分配角色
  GET_USER_INFO_URL = '/admin/acl/user/getUserInfo',// 获取用户信息
  GET_USER_BY_ID_URL = '/admin/acl/user/getUserById',// 根据id获取用户信息
  GET_USER_ROLE_AND_ALL_ROLE_URL = '/admin/acl/user/toAssign',// 查询用户角色列表和全部角色列表
}

// 查询用户列表
export const reqUserList = async (page: number, limit: number) => request.get<any, Response<PageInstanceVO<UserVO>>>(
  `${API.USER_LIST_URL}/${page}/${limit}`
)
export const reqUserListByUserName = async (page: number, limit: number, username: string) => request.get<any, Response<PageInstanceVO<UserVO>>>(
  `${API.USER_LIST_URL}/${page}/${limit}/${username}`
)
// 添加用户
const reqAddUser = async (data: UserVO) => request.post<any, Response<void>>(API.ADD_USER_URL, data)
// 修改用户
const reqUpdateUser = async (data: UserVO) => request.post<any, Response<void>>(API.UPDATE_USER_URL, data)
export const reqSaveOrUpdateUser = async (data: UserVO) => {
  if (!data.id) {
    return reqAddUser(data)
  } else {
    return reqUpdateUser(data)
  }
}

// 删除用户
export const reqRemoveUserByIds = async (ids: number[]) => request.post<any, Response<void>>(API.DELETE_USER_URL, ids)
// 分配角色
export const reqAssignRole = async (data: AssignRoleParam) => request.post<any, Response<void>>(API.ASSIGN_ROLE_URL, data)

export const reqGetUserRoleAndAllRoleList = async (userId: number) => request.get<any, Response<RoleInfoVO>>(`${API.GET_USER_ROLE_AND_ALL_ROLE_URL}/${userId}`)